package com.example.lowbird.lowbird.activity;

import android.content.Context;
import android.content.Intent;

import android.support.design.widget.TabLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.lowbird.lowbird.R;
import com.example.lowbird.lowbird.adapter.Goodadapter;
import com.example.lowbird.lowbird.bean.HotBean;
import com.example.lowbird.lowbird.constant.Contants;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

public class GoodlistActivity extends AppCompatActivity {

    private Intent intent;
    private int id;
    private TextView tv_gooflist;
    private ImageView iv_goodlist;
    private RecyclerView rv_goodlist;
    private Context mContext;
    boolean flag;

    HotBean hotbean = null;
    private int pageSize = 10;
    private int curPage = 1;
    private int orderBy = 1;
    private int campaignId = 1;

    Goodadapter goodadapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goodlist);
        mContext = this;
        intent = getIntent();
        if (intent != null) {
            campaignId = intent.getIntExtra("id", 1);
        }


        initview();
        initdata();

    }

    private void initdata() {

        String url = Contants.API.WARES_CAMPAIN_LIST + "campaignId=" + campaignId + "&orderBy=" + orderBy + "&curPage=" + curPage + "" +
                "&pageSize=" + pageSize;

        OkHttpUtils.get().url(url).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(String response, int id) {
                final HotBean hotBean1 = new Gson().fromJson(response.toString(), HotBean.class);

                rv_goodlist.post(new Runnable() {
                    @Override
                    public void run() {
                        goodadapter.addhotbean(hotBean1);
                        tv_gooflist.setText("共有商品 " + hotBean1.getTotalCount() + "件");
                    }
                });


            }
        });
    }

    private void initview() {




        tv_gooflist = ((TextView) findViewById(R.id.tv_goodlist1));
        iv_goodlist = ((ImageView) findViewById(R.id.iv_goodlistset));

        rv_goodlist = ((RecyclerView) findViewById(R.id.rv_goodlist));
        getdataforrv();
        iv_goodlist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (flag) {

                    rv_goodlist.setLayoutManager(new LinearLayoutManager(mContext));
                    iv_goodlist.setImageResource(android.R.drawable.ic_dialog_dialer);

                    flag=false;
                } else {
                    rv_goodlist.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
                    iv_goodlist.setImageResource(android.R.drawable.ic_menu_sort_by_size);
                    flag=true;

                }

                goodadapter.setlayout();
            }
        });

    }


    private void getdataforrv() {
        goodadapter = new Goodadapter(mContext, hotbean);

        rv_goodlist.setAdapter(goodadapter);


        rv_goodlist.setLayoutManager(new LinearLayoutManager(mContext));


    }
}
